/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Cache;

import com.lcfgly.Dao.CountyImpl;
import com.lcfgly.Dao.UserconuntImpl;
import com.lcfgly.Database.UserConunt;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author lcfgly
 */
@Component
@Aspect
public class UpdateUserCityAop {
   
    @Autowired
    CountyImpl countyImpl;
    
    @Autowired
    UserconuntImpl userconuntImpl;
    
    @Around("execution(* com.lcfgly.Dao.UserconuntImpl.findByUid(..))")  
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
        Object object = pjp.proceed();//执行该方法
        new updateuser((UserConunt) object).start();
        return object;  
    } 
    
    private class updateuser extends Thread{
        
        private final UserConunt userConunt;

        @Override
        public void run() {
            try{
                if(userConunt.getCityid() != null && userConunt.getCityid() != 0 && (userConunt.getCityaddress() ==null || userConunt.getCityaddress().isEmpty())){
                    userConunt.setCityaddress(countyImpl.GetAddress(userConunt.getCityid()));
                    if(userConunt.getLat() == null || userConunt.getLng() ==null || userConunt.getLat().isEmpty() || userConunt.getLng().isEmpty()){
                        Map<String,String> result=getlatlng(userConunt.getCityaddress()+userConunt.getAddress());
                        if(result != null){
                            userConunt.setLat(result.get("lat"));
                            userConunt.setLng(result.get("lng"));
                        }
                    }
                    userconuntImpl.update(userConunt, userConunt.getUid());
                }
            }catch(SQLException e){
                Logger.getLogger("SqlError").error(e);
            }
        }

        public updateuser(UserConunt userConunt) {
            this.userConunt = userConunt;
        }
        
        private Map<String,String> getlatlng(String address){
            String url = "http://api.map.baidu.com/geocoder/v2/?ak=GUk14zGfe4WxVaKLqGWsC8Mz&output=json&address="+address ;
            StringBuilder result=new StringBuilder();
            try {
                URL u= new URL(url);
                HttpURLConnection req = (HttpURLConnection) u.openConnection();
                req.connect();
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        req.getInputStream()));
                String lines;
                while ((lines = reader.readLine()) != null) {
                    result.append(lines);
                }
                reader.close();
            } catch (IOException ex) {
                Logger.getRootLogger().error("HTTP获取数据出错！",ex);
                return null;
            }
            Map res=new HashMap<String,String>();
            ObjectMapper mapper=new ObjectMapper();
            try {
                JsonNode jsonNode=mapper.readTree(result.toString());
                int status=jsonNode.path("status").asInt();
                if(status == 0){
                    JsonNode sNode=jsonNode.path("result").path("location");
                    res.put("lat", sNode.path("lat").asText());
                    res.put("lng", sNode.path("lng").asText());
                }
            } catch (IOException ex) {
                Logger.getRootLogger().error("解析json数据出错！",ex);
            }
            return res;
        }
        
    }
    
}
